import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*; 
import java.io.*;
import java.util.*;
import java.awt.BorderLayout;  
import java.awt.Container;  
import java.io.BufferedReader;
import java.io.File;  
import java.io.InputStreamReader;

import javax.swing.JEditorPane;  
import javax.swing.JFrame;  
import javax.swing.JScrollPane;  

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;

public class ValidateFrame extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6038905784751690357L;
	//	JEditorPane editPane;
	PolicyFrame parent;
	public ValidateFrame(){
		super(new BorderLayout());
		parent=null;
	}
	
	public ValidateFrame(PolicyFrame parent){
		super(new BorderLayout());
		this.parent=parent;
	}
	
	public void refresh(){
		try{

			String projectPath = parent.getProjectPath();
			if (projectPath.equals("")) {
				int i = MainFrame.getInstance().tabPan.getSelectedIndex();
				projectPath = MainFrame.getInstance().tabPan.getTitleAt(i);
				if (projectPath.charAt(0) == '*') {
					projectPath = projectPath.substring(1);
				}
			}
			if (projectPath.length() > 8
					&& projectPath.toLowerCase().endsWith(".p3pproj")) {
				projectPath = projectPath
						.substring(0, projectPath.length() - 8);
			}
			
			/**construct pocily and write to a xml file**/
			File policyfile = new File(projectPath+"$Policy.xml");
			if (policyfile.exists())
				policyfile.delete();
			policyfile.createNewFile();
			parent.flushData();
			parent.xmlPolicy.constructP();
			String policy=parent.xmlPolicy.content;
			FileWriter fw=new FileWriter(policyfile);
			BufferedWriter bw=new BufferedWriter(fw);
			bw.write(policy);
			bw.flush();
			bw.close();
			fw.close();

			/**use httpclient to send xml policy to p3p validator and get response**/
			HttpClient client = new DefaultHttpClient(); 
		   	client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 
		   	HttpPost post = new HttpPost( "http://validator.w3.org/p3p/20020128/policy.pl" );
		   	MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );  
		   	entity.addPart("file", new FileBody(((File)policyfile),"application/zip"));
		   	post.setEntity(entity);
		   	HttpResponse response=client.execute(post);
		   	HttpEntity resentity = response.getEntity();
		   	StringBuffer sb=new StringBuffer("");
		   	if(resentity!=null){
		   		BufferedReader reader = new BufferedReader(new InputStreamReader(resentity.getContent()));
		        String s=reader.readLine();
		        while(s!=null){
					sb.append(s);
					s = reader.readLine();
		         }
		   	}
	
		   	File validatefile=new File(projectPath+"$Validation"+".html");
		   	if(validatefile.exists())
		   		validatefile.delete();
		   	validatefile.createNewFile();
		   	FileWriter fw2=new FileWriter(validatefile);
		   	BufferedWriter bw2=new BufferedWriter(fw2);
		   	bw2.write(sb.toString());
		   	bw2.flush();
		   	bw2.close();
		   	fw2.close();
		   	
		   	removeAll();
		   	JEditorPane editPane = new JEditorPane();
			editPane.setLayout(null);
			editPane.setPreferredSize(new Dimension(650,1000));
			JScrollPane sp = new JScrollPane(editPane); 
			sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			
			editPane.setEditable(false);
			JLabel label=new JLabel("Validation result from http://www.w3.org/P3P/validator.html");
			JPanel p1=new JPanel(new FlowLayout(FlowLayout.LEFT));
			p1.add(label);
			JPanel p2=new JPanel(new BorderLayout());
			p2.add(p1,"North");
			p2.add(sp,"Center");
		    add(p2,"Center");
		   	
//		   	editPane.setPage("http://www.google.com");
		   	editPane.setPage(validatefile.toURI().toURL());
		   	editPane.repaint();
		   	validate();
		   	
		   	client.getConnectionManager().shutdown(); 
		}catch(Exception e){

		}
	}
}
